package com.zhongge.main;

import com.zhongge.user.ProxyUser;
import com.zhongge.user.User;
import com.zhongge.user.factory.AdminUserFactory;
import com.zhongge.user.factory.IUserFactory;
import com.zhongge.user.factory.NormalUserFactory;

import java.util.Scanner;

/**
 * @ClassName LibrarySystem
 * @Description TODO
 * @Author zhongge
 * @Version 1.0
 */
@SuppressWarnings("all")
public class LibrarySystem {
    public static void main(String[] args) {
        //我先创建对象==》得有工厂
        IUserFactory adminUserFactory = new AdminUserFactory();
        IUserFactory normalUserFactory = new NormalUserFactory();

        //创建出管理员对象
        User adminUser = adminUserFactory.createUser("老李", 1);

        //创建出两个普通用户对象
        User normalUser01 = normalUserFactory.createUser("小张", 2);
        User normalUser02 = normalUserFactory.createUser("小王", 3);

        //使用代理类来代理这三个对象：首先你得有代理类
        ProxyUser proxyAdminUser = new ProxyUser(adminUser);//代理管理员
        ProxyUser proxyNormalUser01 = new ProxyUser(normalUser01);//代理普通用户01
        ProxyUser proxyNormalUser02 = new ProxyUser(normalUser02);//代理普通用户02

        //测试：通过代理类来得到用户选择的结果：
        /*System.out.println(proxyAdminUser.getRealUser().menu());
        System.out.println(proxyNormalUser01.getRealUser().menu());
        System.out.println(proxyNormalUser02.getRealUser().menu());
        */

        //选择用户确定代理类代理的具体对象（只需选择一次）
        ProxyUser proxyUser = selectProxyUser(proxyAdminUser, proxyNormalUser01, proxyNormalUser02);
        while (true) {
            //得到了具体代理对象之后根据代理对象给出用户具体的操作清单
            int choice = proxyUser.getRealUser().menu();//返回一个具体的操作选择choice

            //根据choice选择去访问具体的操作
            proxyUser.selectionUserFunctions(choice);//用户功能的选择
        }

    }

    /**
     * 根据用户输入返回 具体 的代理对象
     * @param proxyAdminUser 管理员代理对象
     * @param proxyNormalUser01 普通用户01代理对象
     * @param proxyNormalUser02 普通用户02代理对象
     * @return
     */
    public static ProxyUser selectProxyUser(ProxyUser proxyAdminUser,
                                            ProxyUser proxyNormalUser01,
                                            ProxyUser proxyNormalUser02){
        Scanner sc = new Scanner(System.in);
        System.out.println("**************************");
        System.out.println("请选择用户角色:");
        System.out.println("1. 管理员");
        System.out.println("2. 普通用户(小张)");
        System.out.println("3. 普通用户(小王)");
        System.out.println("4. 退出系统");
        System.out.println("**************************");
        int choice = sc.nextInt();
        switch(choice) {
            case 1:
                return proxyAdminUser;
            case 2:
                return proxyNormalUser01;
            case 3:
                return proxyNormalUser02;
            case 4:
                System.exit(0);
            default:
                System.out.println("输入有误请重新输入！！！");
                return null;
        }
    }
}